package com.example.lin.widgettest_327ljd;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv;
    private EditText et;
    private Button btn1,btn2,btn3,btn4;
    private ImageButton imbtn;
    private ImageView im;
    private RadioGroup rg;
    private RadioButton rbb,rbg;
    private CheckBox cbb,cbf,cbs;

    private  String strfun="";//兴趣字符串

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init(){//获取控件
        tv=(TextView)findViewById(R.id.textView);
        et=(EditText)findViewById(R.id.editText);
        btn1=(Button) findViewById(R.id.button);
        btn2=(Button) findViewById(R.id.button2);
        btn3=(Button) findViewById(R.id.button3);
        btn4=(Button) findViewById(R.id.button4);
        im=(ImageView)findViewById(R.id.imageView);
        imbtn=(ImageButton)findViewById(R.id.imageButton);

        rg=(RadioGroup)findViewById(R.id.radiogroup);
        rbb=(RadioButton)findViewById(R.id.radioButton);
        rbg=(RadioButton)findViewById(R.id.radioButton2);
        cbb=(CheckBox)findViewById(R.id.checkBox);
        cbf=(CheckBox)findViewById(R.id.checkBox2);
        cbs=(CheckBox)findViewById(R.id.checkBox3);
        //设置按钮监听器
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        imbtn.setOnClickListener(this);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId)
                {
                    case R.id.radioButton://男
                        tv.setText("男");
                        break;
                    case R.id.radioButton2://女
                        tv.setText("女");
                        break;
                }
            }
        });
        cbb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfun+="篮球";
                else
                    strfun=strfun.replace("篮球+"," ");
                tv.setText(strfun);
            }
        });
        //足球选择事件
        cbf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfun+="足球";
                else
                    strfun=strfun.replace("足球+"," ");
                tv.setText(strfun);
            }
        });
        //游泳选择事件
        cbs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfun+="游泳";
                else
                    strfun=strfun.replace("游泳+"," ");
                tv.setText(strfun);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button://设置文本
                String str=et.getText().toString();
                tv.setText(str);
                break;
            case R.id.button2://设置图片
                im.setImageResource(R.drawable.b);
                break;
            case R.id.imageButton://图片按钮
                Intent intent=new Intent(MainActivity.this,NewActivity.class);
                intent.putExtra("param1",et.getText().toString());
                intent.putExtra("param2",3);
                startActivity(intent);
                break;
            case R.id.button3:
                if(rbb.isChecked())
                    tv.setText("男");
                if(rbg.isChecked())
                    tv.setText("女");
                break;
            case R.id.button4:
                String msg=" ";
                if(cbb.isChecked()) msg+="篮球";
                if(cbf.isChecked()) msg+="足球";
                if(cbs.isChecked()) msg+="游泳";
                tv.setText(msg);
                break;
        }

    }
}
